const https = require("../../../utils/https");

Page({

  data: {
    address: '郑州市 二七区 XX路XX号',
    selfAddress: [],
    addressId: 0,
  },

  onChangesAddress(e) {
    console.log(e.detail.value);
    var list = this.data.selfAddress
    for (var i = 0; i < list.length; i++) {
      if (list[i].id == e.detail.value) {
        wx.setStorageSync('selfId', e.detail.value)
        this.setData({
          address: list[i].name,
          addressId: e.detail.value
        })
        break
      }
    }
  },

  onLoad(options) {
    var id = wx.getStorageSync('selfId')||null
    

    var that = this
    // 获取自提点的地址
    https.GET('/tool/pointpick/listAll', {}, (res) => {
      console.log('成功:', res);
      if (res.code === 200) {
        that.setData({
          selfAddress: res.rows
        })
        var list = res.rows
        var indexId = 0
        for (var i = 0; i < list.length; i++) {
          list[i].isDefault = false
          if(id==list[i].id){
            indexId=i
          }
        }
        list[indexId].isDefault = true
        that.setData({
          selfAddress: list,
          address: list[0].name,
          addressId:list[0].id
        })
      }
    }, (err) => {
      console.error('失败:', err);
    });
  },
  onShow() {},
  // 当前页面的返回按钮点击事件
  onBackTap() {
    // 获取页面栈
    const pages = getCurrentPages();
    // 上一页的索引是 pages.length - 2
    const prevPage = pages[pages.length - 2];

    // 给上一页添加参数（直接修改上一页的data）
    console.log("addressId",this.data.addressId);
    prevPage.setData({
      returnValue: this.data.addressId
    });

    // 执行返回操作
    wx.navigateBack({
      delta: 1 // 返回的页面层数，1表示返回上一页
    });
  }
})